﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
#include<time.h>
#define Count 5
#define Col 40//列
#define Row 40//行
//玩家飞机坐标
int PlayerPlane_y = Col - 2;
int PlayerPlane_x = Row / 2 - 1;
//子弹坐标
int Bullet_y;
int Bullet_x;
//敌机坐标
int Enemy_y[Count] = { 0 };
int Enemy_x[Count] = { 0 };
//敌机的移动速度
int EnemySleep = 20;
int sleep = 0;
//分数
int score = 0;
//技能充能
int skill1 = 0;
int skill2 = 5;
void menu()
{
	printf("--------------飞机大作战--------------\n");
	printf("|                                    |\n");
	printf("|             1.开始游戏             |\n");
	printf("|             0.退出游戏             |\n");
	printf("|             W/A/S/D移动            |\n");
	printf("|           空格射击 E/R技能         |\n");
	printf("|                                    |\n");
	printf("--------------------------------------\n");
}

//隐藏光标
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };  //第二个值为0，表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

// 光标移到(X, Y)位置
void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void DisPlay(int arr[Col][Row])
{
	gotoxy(0, 0);
	for(int i=0;i<Col;i++)
	{
		for (int j = 0; j < Row; j++)
		{
			if (arr[i][j] == 0)
			{
				printf(" ");
			}
			if (arr[i][j] == 1)
			{
				printf("█");
			}
			if (arr[i][j] == 2)
			{
				printf("*");
			}
			if (arr[i][j] == 3)
			{
				printf("$");
			}
			if (arr[i][j] == 4)
			{
				printf("^");
			}
		}
		printf("\n");
	}
	printf("得分：%d ", score);
	printf("EnemySleep=%d ", EnemySleep);
	printf("skill1(20)=%d ", skill1);
	printf("skill2(5)=%d \n", skill2);
	Sleep(20);
}

void InSet(int arr[Col][Row])
{
	srand(time(NULL));
	//路--0
	//墙--1
	for (int i = 0; i < Col; i++)
	{
		arr[i][0] = 1;
		arr[i][Row - 1] = 1;
	}
	for (int i = 0; i < Row; i++)
	{
		arr[0][i] = 1;
		arr[Col - 1][i] = 1;
	}
	//玩家飞机--2
	arr[PlayerPlane_y][PlayerPlane_x] = 2;
	//敌机--3
	for (int i = 0; i < Count; i++)
	{
		Enemy_y[i] = rand() % 3 + 1;
		Enemy_x[i] = rand() % Row + 1;
		if (Enemy_x[i] >= Row - 1)
			Enemy_x[i] -= 2;
		arr[Enemy_y[i]][Enemy_x[i]] = 3;
	}
	//子弹--4
}
void PlayerPlay(int arr[Col][Row])
{
	if (_kbhit())//判断是否有按键按下
	{
			char ch = getch();
			if (ch == 'w' && arr[PlayerPlane_y - 1][PlayerPlane_x] == 0)
			{
				arr[PlayerPlane_y][PlayerPlane_x] = 0;
				PlayerPlane_y--;
				arr[PlayerPlane_y][PlayerPlane_x] = 2;
			}
			if (ch == 'a' && arr[PlayerPlane_y][PlayerPlane_x - 1] == 0)
			{
				arr[PlayerPlane_y][PlayerPlane_x] = 0;
				PlayerPlane_x--;
				arr[PlayerPlane_y][PlayerPlane_x] = 2;
			}
			if (ch == 's' && arr[PlayerPlane_y + 1][PlayerPlane_x] == 0)
			{
				arr[PlayerPlane_y][PlayerPlane_x] = 0;
				PlayerPlane_y++;
				arr[PlayerPlane_y][PlayerPlane_x] = 2;
			}
			if (ch == 'd' && arr[PlayerPlane_y][PlayerPlane_x + 1] == 0)
			{
				arr[PlayerPlane_y][PlayerPlane_x] = 0;
				PlayerPlane_x++;
				arr[PlayerPlane_y][PlayerPlane_x] = 2;
			}
			if (ch == ' ')
			{
				Bullet_y = PlayerPlane_y - 1;
				Bullet_x = PlayerPlane_x;
				arr[Bullet_y][Bullet_x] = 4;
			}
			if (ch == 'r')
			{
				if (skill1 == 0)
				{
					for (int i = 1; i < Row - 1; i++)
					{
						skill1 = 0;
						Bullet_y = PlayerPlane_y - 1;
						Bullet_x = i;
						arr[Bullet_y][Bullet_x] = 4;
					}
				}
			}
			if (ch == 'e')
			{
				int left = PlayerPlane_x - 3;
				int right = PlayerPlane_x + 3;
				if (skill2 == 5)
				{
					for (int i = left; i < right; i++)
					{
						if (i > 0 && i < Row - 1)
						{
							skill2 = 0;
							Bullet_y = PlayerPlane_y - 1;
							Bullet_x = i;
							arr[Bullet_y][Bullet_x] = 4;
						}
					}
				}
			}
	}
}
void BulletEnemy(int arr[Col][Row])
{
	
	for (int i = 0; i < Col; i++)
	{
		for (int j = 0; j < Row; j++)
		{
			if (arr[i][j] == 4)
			{
				for (int k = 0; k < Count; k++)
				{
					//子弹击中敌机的处理
					if (i == Enemy_y[k] && j == Enemy_x[k])
					{
						/*if (skill1 < 20)
						{
							skill1++;
						}*/
						if (skill2 < 5)
						{
							skill2++;
						}
						score += 100;
						arr[Enemy_y[k]][Enemy_x[k]] = 0;
						Enemy_y[k] = rand() % 3 + 1;
						Enemy_x[k] = rand() % Row + 1;
						if (Enemy_x[k] >= Row - 1)
							Enemy_x[k] -= 2;
						arr[Enemy_y[k]][Enemy_x[k]] = 3;
						//每2000分敌机加速
						if (score % 2000 == 0 && EnemySleep > 2)
						{
							EnemySleep -= 2;
						}
					}
				}
				
				//子弹的移动
				if (arr[i][j] == 4)
				{
					arr[i][j] = 0;
					if (i > 1)
					{
						arr[i - 1][j] = 4;
					}
				}
			}
		}
	}
	//敌机的速度
	if (sleep < EnemySleep)
	{
		sleep++;
	}
	else if (sleep > EnemySleep)
	{
		sleep = 0;
	}

	for (int i = 0; i < Count; i++)
	{
		
		if (PlayerPlane_y == Enemy_y[i] && PlayerPlane_x == Enemy_x[i] || score < 0)
		{
			printf("  /\\_/\\  \n");//敌机击中玩家飞机的处理
			printf(" ( o.o ) \n");
			printf("  > ^ < \n");
			printf("游戏失败！\n");
			printf("\a");
			system("pause");
			exit(0);
		}
		//敌机到达最底面的处理
		if (Enemy_y[i] >= Col - 2)
		{
			score -= 100;
			arr[Enemy_y[i]][Enemy_x[i]] = 0;
			Enemy_y[i] = rand() % 3 + 1;
			Enemy_x[i] = rand() % Row + 1;
			if (Enemy_x[i] >= Row - 1)
				Enemy_x[i] -= 2;
			arr[Enemy_y[i]][Enemy_x[i]] = 3;
		}
		//敌机下移的处理
		if (sleep == EnemySleep)
		{
			for (int j = 0; j < Count; j++)
			{
				arr[Enemy_y[j]][Enemy_x[j]] = 0;
				sleep = 0;
				Enemy_y[j]++;
				arr[Enemy_y[j]][Enemy_x[j]] = 3;
			}
		}
	}
	
}

void game()
{
	//设置一个存放信息的数组
	int arr[Col][Row] = { 0 };
	//隐藏光标
	HideCursor();
	//放置信息
	InSet(arr);
	//打印游戏界面
	DisPlay(arr);
	//玩家移动
	while (1)
	{
		//玩家操作
		PlayerPlay(arr);
		//打印棋盘
		DisPlay(arr);
		//子弹与敌机的操作
		BulletEnemy(arr);
	}
}
int main()
{
	system("color b");
	int input = 0;
	menu();
	printf("请选择:");
	scanf("%d", &input);
	switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入有误，请重新输入:\n");
			break;
		}
	return 0;
}